//
//  HWWelfareFuncCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/4/2.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWWelfareFuncCell: UITableViewCell {
    var itemViews:[HWWelfareFuncItemView] = []
    var lineView:UIView!
    var model:HWWelfareCenterModel! {
        didSet {
            itemViews.removeAll()
    
            var tempView:HWWelfareFuncItemView!
            for i in 0..<model.maindata.count {
                let itemView = HWWelfareFuncItemView()
                itemView.imageView.sd_setImage(with: URL(string: model.maindata[i].icon), completed: nil)
                itemView.titleLabel.text = model.maindata[i].name
                itemView.tag = i
                itemView.itemClick = {
                    self.itemClick?(self.model.maindata[itemView.tag])
                }
                contentView.addSubview(itemView)
                if tempView != nil {
                    if i % 4 == 0 {
                        itemView.snp.makeConstraints { make in
                            make.left.equalToSuperview()
                            make.top.equalTo(tempView.snp.bottom)
                            make.width.equalTo(screenWidth/4)
                            make.height.equalTo(61*widthScale)
                        }
                    }else {
                        itemView.snp.makeConstraints { make in
                            make.left.equalTo(tempView.snp.right)
                            make.top.equalTo(tempView)
                            make.width.equalTo(screenWidth/4)
                            make.height.equalTo(61*widthScale)
                        }
                    }
                }else {
                    itemView.snp.makeConstraints { make in
                        make.left.equalToSuperview()
                        make.top.equalToSuperview()
                        make.width.equalTo(screenWidth/4)
                        make.height.equalTo(61*widthScale)
                    }
                }

                tempView = itemView
                itemViews.append(itemView)
            }
            if itemViews.count > 0 {
                lineView.snp.remakeConstraints { make in
                    make.top.equalTo(itemViews.last!.snp.bottom).offset(20*widthScale)
                    make.left.right.bottom.equalToSuperview().priority(.low)
                    make.height.equalTo(2*widthScale)
                }
            }
        }
    }
    
    var itemClick:((_:HWWelfareCenterDataModel)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWWelfareFuncCell) {
        let reUse:String = "HWWelfareFuncCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWWelfareFuncCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWWelfareFuncCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        contentView.addSubview(lineView)
    }
    
    func initFrame() -> Void {
        lineView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(2*widthScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
